package com.ternnetwork.oa.controller.note;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.oa.dto.note.NoteCategoryDto;
import com.ternnetwork.oa.service.note.NoteCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;


@Controller
@RequestMapping("/note/category/*")
public class NoteCategoryController {

    @Autowired
    private NoteCategoryService noteCategoryService;


    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_CATEGORY_ADD')")
    @RequestMapping("/add")
    public @ResponseBody BaseResponse add(String requestJson, Principal user) throws Exception {
        NoteCategoryDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, NoteCategoryDto.class);
        return noteCategoryService.add(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_CATEGORY_UPDATE')")
    @RequestMapping("/update")
    public @ResponseBody BaseResponse update(String requestJson, Principal user) throws Exception {
        NoteCategoryDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, NoteCategoryDto.class);
        return noteCategoryService.update(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_CATEGORY_FIND_MY_ALL')")
    @RequestMapping("/findMyAll")
    public @ResponseBody BaseResponse findMyAll(Principal user)  {
      return noteCategoryService.findAll(user.getName());
    }

    @PreAuthorize("hasAnyAuthority('API_APP_OA_NOTE_CATEGORY_DELETE_BY_ID')")
    @RequestMapping("/deleteById")
    public @ResponseBody BaseResponse deleteById(Long id, Principal user) {
        return noteCategoryService.deleteById(id,user.getName());
    }



}
